package br.edu.ifpb.calculator;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;


/**
 * Implementação do Serviço da Calculadora
 * 
 * 
 * @author ari
 *
 */
public class ServiceImpl extends UnicastRemoteObject implements Service {
	private static final long serialVersionUID = 1L;
	
	/**
	 * Evitar erro de envio de parametro nulo ou sem valores;
	 * 
	 * @param params
	 * @return
	 */
	private int[] validate(int[] params){
		if (params != null && params.length > 0){
			return params;
		}
		else {
			return new int[]{0}; 
		}
	}

	public ServiceImpl() throws RemoteException {
		super();
	}

	@Override
	public int sum(int[] values) throws RemoteException {
		int[] vs = validate(values);
		int s = vs[0];
		for (int ix = 1; ix < vs.length; ix++){
			s += vs[ix];
		}
		return s;
	}

	@Override
	public int diff(int[] values) throws RemoteException {
		int[] vs = validate(values);
		int s = vs[0];
		for (int ix = 1; ix < vs.length; ix++){
			s -= vs[ix];
		}
		return s;
	}

	@Override
	public int mult(int[] values) throws RemoteException {
		int[] vs = validate(values);
		int s = vs[0];
		for (int ix = 1; ix < vs.length; ix++){
			s *= vs[ix];
		}
		return s;
	}

	@Override
	public int div(int[] values) throws RemoteException {
		int[] vs = validate(values);
		int s = vs[0];
		for (int ix = 1; ix < vs.length; ix++){
			s /= vs[ix];
		}
		return s;
	}

}
